package vn.com.pavibook.admin.controller;

import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import vn.com.pavibook.admin.service.ManageBookTypeService;
import vn.com.pavibook.domain.model.BookType;
import vn.com.pavibook.domain.model.PagingInfo;
import vn.com.pavibook.domain.model.SearchResult;
import vn.com.pavibook.model.vo.BookTypeVO;

@Controller
public class ManageBookTypeController {

	private static final int offset = 10;
	
	private static final Logger logger = LoggerFactory.getLogger(ManageBookController.class);
	
	@Value("${addBookType.title}")
	private String addBookTypeTitle;

	@Value("${editBookType.title}")
	private String editBookTypeTitle;
	
	@Autowired
	private ManageBookTypeService manageBookTypeService;
	
	//info
	@RequestMapping(value = "/admin/booktype-info", method=RequestMethod.GET)
	public String openManageBookType(Model model){
		logger.debug("manage booktype");
		
		PagingInfo pagingInfo = new PagingInfo(offset, 1);
		SearchResult<BookType> result = manageBookTypeService.getBookType(null, pagingInfo);
		List<BookType> listBookType = result.getSearchResult();
		PagingInfo pagingInfoNew = result.getPagingInfo();
		model.addAttribute("listBookType", listBookType);
		model.addAttribute("pagingInfo", pagingInfoNew);
		
		return "admin/booktype-info";
	}
	
	//search
	@RequestMapping(value = "/admin/searchBookType", method = RequestMethod.POST)
	public String searchPublisher(Model model, 
			@RequestParam("pageIndex") int pageIndex, @RequestParam("keyword") String keyword) {
		
		PagingInfo pagingInfo = new PagingInfo(offset, pageIndex);
		SearchResult<BookType> result = new SearchResult<BookType>();
		if(StringUtils.isEmpty(keyword)){
			result = manageBookTypeService.getBookType(null, pagingInfo);
		} else {
			result = manageBookTypeService.getBookType(keyword, pagingInfo);
		}
		List<BookType> listBookType = result.getSearchResult();
		
		model.addAttribute("listBookType", listBookType);
		model.addAttribute("pagingInfo", pagingInfo);
		model.addAttribute("keyword", keyword);
		return "admin/booktype-info";
	}
	
	//delete
	@RequestMapping(value = "/admin/delete-booktype", method=RequestMethod.GET)
	public String deleteBook(Model model, @RequestParam("booktypeId") int booktypeId){
		logger.debug("controller delete book type");
		manageBookTypeService.deleteBookType(booktypeId);
		return "redirect:booktype-info";
	}
	
	//create add new
	@RequestMapping(value = "/admin/add-booktype", method=RequestMethod.GET)
	public String createAddNewBookType(Model model){
		logger.debug("controller createAddNewBookType");
		BookTypeVO bookTypeVO = new BookTypeVO();
		model.addAttribute("bookTypeVO", bookTypeVO);
		model.addAttribute("titlePage", addBookTypeTitle);
		return "admin/add-booktype";
	}
	
	//create add new
	@RequestMapping(value = "/admin/edit-booktype", method=RequestMethod.GET)
	public String editBookType(Model model, @RequestParam("bookTypeId") int bookTypeId){
		logger.debug("controller edit BookType");
		
		BookTypeVO bookTypeVO = new BookTypeVO();
		bookTypeVO = manageBookTypeService.getBookTypeById(bookTypeId);
		model.addAttribute("bookTypeVO", bookTypeVO);
		model.addAttribute("titlePage", editBookTypeTitle);
		return "admin/add-booktype";
	}
	
	//save add new
	@RequestMapping(value = "/admin/save-booktype")
	public String saveBook(Model model, @ModelAttribute("bookTypeVO") @Valid BookTypeVO bookTypeVO, BindingResult result){
		logger.debug("controller saveBooktype");
		if(result.hasErrors()){
			return "admin/add-booktype";
		}
		
		boolean isEdit = false;
		if(bookTypeVO.getId() != 0){
			isEdit = true;
		}
		if(!isEdit){
			boolean isExistedBooktype = manageBookTypeService.isExistedBooktype(bookTypeVO.getName());
			if(isExistedBooktype){
				model.addAttribute("nameExited", true);
				return "admin/add-booktype";
			}
			manageBookTypeService.addNewBookType(bookTypeVO);
		}else{
			manageBookTypeService.editBookType(bookTypeVO);
		}
		model.addAttribute("addStatus", true);
		return "admin/add-booktype";
	}
}
